package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Projects;
import com.ruoyi.system.service.IProjectsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * projectsController
 * 
 * @author ruoyi
 * @date 2025-08-05
 */
@RestController
@RequestMapping("/system/projects")
public class ProjectsController extends BaseController
{
    @Autowired
    private IProjectsService projectsService;

    /**
     * 查询projects列表
     */
    @PreAuthorize("@ss.hasPermi('system:projects:list')")
    @GetMapping("/list")
    public TableDataInfo list(Projects projects)
    {
        startPage();
        List<Projects> list = projectsService.selectProjectsList(projects);
        return getDataTable(list);
    }

    /**
     * 导出projects列表
     */
    @PreAuthorize("@ss.hasPermi('system:projects:export')")
    @Log(title = "projects", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Projects projects)
    {
        List<Projects> list = projectsService.selectProjectsList(projects);
        ExcelUtil<Projects> util = new ExcelUtil<Projects>(Projects.class);
        util.exportExcel(response, list, "projects数据");
    }

    /**
     * 获取projects详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:projects:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(projectsService.selectProjectsById(id));
    }

    /**
     * 新增projects
     */
    @PreAuthorize("@ss.hasPermi('system:projects:add')")
    @Log(title = "projects", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Projects projects)
    {
        return toAjax(projectsService.insertProjects(projects));
    }

    /**
     * 修改projects
     */
    @PreAuthorize("@ss.hasPermi('system:projects:edit')")
    @Log(title = "projects", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Projects projects)
    {
        return toAjax(projectsService.updateProjects(projects));
    }

    /**
     * 删除projects
     */
    @PreAuthorize("@ss.hasPermi('system:projects:remove')")
    @Log(title = "projects", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(projectsService.deleteProjectsByIds(ids));
    }


    @Log(title = "添加项目", businessType = BusinessType.INSERT)
    @PostMapping("/addcustom")
    @ResponseBody
    public AjaxResult addCustom(@Validated @RequestBody Projects projects)
    {
        projects.setId(UUID.randomUUID().toString());
        projects.setDeleted("0");
        if(projects.getNumber() == null){
            projects.setNumber(new Long(projectsService.getMaxNumber() + 1));
        }
        projects.setCreateTime(new Date());
        projects.setUpdateTime(new Date());
        LoginUser loginUser = getLoginUser();
        projects.setUpdateBy(loginUser.getUsername());

        return toAjax(projectsService.insertProjects(projects));
    }

    @Log(title = "修改项目", businessType = BusinessType.UPDATE)
    @PostMapping("/modify")
    @ResponseBody
    public AjaxResult modify(@Validated @RequestBody Projects projects)
    {
        Projects oldProjects = projectsService.selectProjectsById(projects.getId());
        projects.setCreateTime(oldProjects.getCreateTime());

        projects.setUpdateTime(new Date());

        LoginUser loginUser = getLoginUser();
        projects.setUpdateBy(loginUser.getUsername());

        return toAjax(projectsService.updateProjects(projects));
    }

    @Log(title = "删除项目", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ResponseBody
    public AjaxResult delete(@RequestParam("id") String id)
    {
        Projects projects = projectsService.selectProjectsById(id);

        projects.setUpdateTime(new Date());
        projects.setDeleted("1");

        return toAjax(projectsService.updateProjects(projects));
    }

    @GetMapping("/selectall")
    @ResponseBody
    public Map<String, Object> selectall(@RequestParam("projectname") String ppprojectname,@RequestParam("projecttype") String ppprojecttype, @RequestParam("pageindex") int ppPageIndex, @RequestParam("pagesize") int ppPageSize)
    {
        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
        if (!"".equals(ppprojectname)){
            ppprojectname = "%"+ppprojectname+"%";
        }

        int mmCount = projectsService.countAll(ppprojectname,ppprojecttype);
        Map<String, Object> mmMap = new HashMap<String, Object>();
        PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
        List<Projects> mmInfo = projectsService.selectAll( ppprojectname,ppprojecttype, mmStartIndex, ppPageSize);
        mmMap.put("InfoList", mmInfo);
        mmMap.put("PageInfo", mmPageInfo);
        return mmMap;
    }

    @GetMapping("/selectValid")
    @ResponseBody
    public Map<String, Object> selectValid(@RequestParam("projectname") String ppprojectname,@RequestParam("projecttype") String ppprojecttype, @RequestParam("fabushijian") String ppfabushijian, @RequestParam("publishingunit") String pppublishingunit,@RequestParam("paixu") String pppaixu, @RequestParam("pageindex") int ppPageIndex, @RequestParam("pagesize") int ppPageSize)
    {
        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
        if (!"".equals(ppprojectname)){
            ppprojectname = "%"+ppprojectname+"%";
        }

        int mmCount = projectsService.countValid(ppprojectname,ppprojecttype, ppfabushijian, pppublishingunit);
        Map<String, Object> mmMap = new HashMap<String, Object>();
        PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
        List<Projects> mmInfo = projectsService.selectValid( ppprojectname,ppprojecttype, ppfabushijian, pppublishingunit,pppaixu, mmStartIndex, ppPageSize);
        mmMap.put("InfoList", mmInfo);
        mmMap.put("PageInfo", mmPageInfo);
        return mmMap;
    }

    @GetMapping("/findone")
    @ResponseBody
    public Projects findone(@RequestParam("id") String id)
    {
        Projects projects = projectsService.selectProjectsById(id);
        return projects;
    }
}
